//
//  LZSelectTableViewCell.m
//  laziz_Merchant
//
//  Created by ZhaoDongBo on 2016/12/20.
//  Copyright © 2016年 XBN. All rights reserved.
//

#import "LZSelectTableViewCell.h"
#import "Masonry.h"

#import "LZCouponListModel.h"

@interface LZSelectTableViewCell ()

@property (nonatomic, strong) UILabel * titleLabel;

@property (nonatomic, strong) UIButton * restaurantBtn;
@property (nonatomic, strong) UIImageView * restaurantImage;
@property (nonatomic, strong) UILabel * restaurantlabel;

@property (nonatomic, strong) UIButton * xbnBtn;
@property (nonatomic, strong) UIImageView * xbnImage;
@property (nonatomic, strong) UILabel * xbnlabel;

@property (nonatomic, strong) UIView * line;

@end

@implementation LZSelectTableViewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        self.backgroundColor = UIColorFromRGB(0xffffff, 1);
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        [self configView];
        [self addAutoLayoutToCell];
    }
    return self;
}

- (void)configView {
    
    [self.contentView addSubview:self.titleLabel];
    
    [self.contentView addSubview:self.restaurantBtn];
    [self.restaurantBtn addSubview:self.restaurantImage];
    [self.restaurantBtn addSubview:self.restaurantlabel];
    
    [self.contentView addSubview:self.xbnBtn];
    [self.xbnBtn addSubview:self.xbnImage];
    [self.xbnBtn addSubview:self.xbnlabel];
    
    [self.contentView addSubview:self.line];
}

- (void)buttonAction:(UIButton *)sender {
    
    NSInteger value = -1;
    
    if (sender.tag == 100) {
        value = 1;
    }
    else if (sender.tag == 200) {
        value = 0;
    }
    
    if (self.selectButtonActionBlock) {
        self.selectButtonActionBlock(value);
    }
}

- (void)addAutoLayoutToCell {
    
    WS(weakSelf);
    
    [self.titleLabel makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(15);
        make.leading.mas_equalTo(10);
        make.trailing.mas_equalTo(10);
        make.height.mas_equalTo(20);
    }];
    
    [self.restaurantBtn makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf.titleLabel.mas_bottom).with.offset(15);
        make.leading.mas_equalTo(10);
        make.width.mas_equalTo(SCREEN_WIDTH/2.0-10);
        make.height.mas_equalTo(30);
    }];
    
    [self.restaurantImage makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(weakSelf.restaurantBtn.mas_centerY);
        make.leading.mas_equalTo(0);
        make.size.mas_equalTo(CGSizeMake(20, 20));
    }];
    
    [self.restaurantlabel makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(weakSelf.restaurantBtn.mas_centerY);
        make.leading.mas_equalTo(weakSelf.restaurantImage.mas_trailing).with.offset(10);
        make.trailing.mas_equalTo(-10);
        make.height.mas_equalTo(30);
    }];
    
    [self.xbnBtn makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf.titleLabel.mas_bottom).with.offset(15);
        make.leading.mas_equalTo(weakSelf.restaurantBtn.mas_trailing).with.offset(10);
        make.trailing.mas_equalTo(-10);
        make.height.mas_equalTo(30);
    }];
    
    [self.xbnImage makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(weakSelf.xbnBtn.mas_centerY);
        make.leading.mas_equalTo(0);
        make.size.mas_equalTo(CGSizeMake(20, 20));
    }];
    
    [self.xbnlabel makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(weakSelf.xbnBtn.mas_centerY);
        make.leading.mas_equalTo(weakSelf.xbnImage.mas_trailing).with.offset(10);
        make.trailing.mas_equalTo(-10);
        make.height.mas_equalTo(30);
    }];
    
    [self.line makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(0.5);
        make.leading.mas_equalTo(10);
        make.trailing.mas_equalTo(-10);
        make.height.mas_equalTo(0.6);
    }];
}

- (UILabel *)titleLabel {
    
    if (_titleLabel == nil) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.font = [UIFont systemFontOfSize:15];
        _titleLabel.textColor = UIColorFromRGB(0x333333, 1);
        if ([LZLanguageManager shareInstance].getLanguageType == LanguageType_Ar) {
            _titleLabel.textAlignment = NSTextAlignmentRight;
        } else {
            _titleLabel.textAlignment = NSTextAlignmentLeft;
        }
    }
    
    return _titleLabel;
}

- (UIButton *)restaurantBtn {
    
    if (_restaurantBtn == nil) {
        _restaurantBtn = [[UIButton alloc] init];
        _restaurantBtn.tag = 100;
        [_restaurantBtn addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _restaurantBtn;
}

- (UIImageView *)restaurantImage {
    
    if (_restaurantImage == nil) {
        _restaurantImage = [[UIImageView alloc] init];
        _restaurantImage.image = [UIImage imageNamed:@"lz_select_no"];
    }
    
    return _restaurantImage;
}

- (UILabel *)restaurantlabel {
    if (_restaurantlabel == nil) {
        _restaurantlabel = [[UILabel alloc] init];
        _restaurantlabel.font = [UIFont systemFontOfSize:15];
        _restaurantlabel.textColor = UIColorFromRGB(0x333333, 1);
        _restaurantlabel.text = LZLocalizedString(@"review_restaurant", @"");
        if ([LZLanguageManager shareInstance].getLanguageType == LanguageType_Ar) {
            _restaurantlabel.textAlignment = NSTextAlignmentRight;
        } else {
            _restaurantlabel.textAlignment = NSTextAlignmentLeft;
        }
    }
    return _restaurantlabel;
}

- (UIButton *)xbnBtn {
    
    if (_xbnBtn == nil) {
        _xbnBtn = [[UIButton alloc] init];
        _xbnBtn.tag = 200;
        [_xbnBtn addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _xbnBtn;
}

- (UIImageView *)xbnImage {
    if (_xbnImage == nil) {
        _xbnImage = [[UIImageView alloc] init];
        _xbnImage.image = [UIImage imageNamed:@"lz_select_no"];
    }
    return _xbnImage;
}

- (UILabel *)xbnlabel {
    if (_xbnlabel == nil) {
        _xbnlabel = [[UILabel alloc] init];
        _xbnlabel.font = [UIFont systemFontOfSize:15];
        _xbnlabel.textColor = UIColorFromRGB(0x333333, 1);
        _xbnlabel.text = @"XBN";
        if ([LZLanguageManager shareInstance].getLanguageType == LanguageType_Ar) {
            _xbnlabel.textAlignment = NSTextAlignmentRight;
        } else {
            _xbnlabel.textAlignment = NSTextAlignmentLeft;
        }
    }
    return _xbnlabel;
}

- (UIView *)line {
    
    if (_line == nil) {
        _line = [[UIView alloc] init];
        _line.backgroundColor = UIColorFromRGB(0xeeeeee, 1);
    }
    
    return _line;
}

- (void)setCouponListModel:(LZCouponListModel *)couponListModel andTag:(NSInteger)index {

    self.titleLabel.text = LZLocalizedString(@"fund_provider", @"");
    
    if (couponListModel) {
        _couponListModel = couponListModel;
        
//        if (index == 2) {
//            if (couponListModel.couponsOrigin == 0) {
//                self.restaurantImage.image = [UIImage imageNamed:@"lz_select_no"];
//                self.xbnImage.image = [UIImage imageNamed:@"lz_select_yes"];
//            }
//            else if (couponListModel.couponsOrigin == 1) {
//                self.restaurantImage.image = [UIImage imageNamed:@"lz_select_yes"];
//                self.xbnImage.image = [UIImage imageNamed:@"lz_select_no"];
//            }
//        }
//        
//        if (index == 4) {
//            if (couponListModel.grouponOrigin == 0) {
//                self.restaurantImage.image = [UIImage imageNamed:@"lz_select_no"];
//                self.xbnImage.image = [UIImage imageNamed:@"lz_select_yes"];
//            }
//            else if (couponListModel.grouponOrigin == 1) {
//                self.restaurantImage.image = [UIImage imageNamed:@"lz_select_yes"];
//                self.xbnImage.image = [UIImage imageNamed:@"lz_select_no"];
//            }
//        }
    }
}

@end
